Vue.component("login",{
    template:`
    <span>
        <slot name="nologin" v-if="myinfo == null">
            <span>
                <a href="#" @click.prevent="showLoginWin=true">登录</a>
                <a href="#">注册</a>
                <a href="#">忘记密码</a>
            </span>
        </slot>
        <slot name="logined" v-else :user="myinfo" :showLogin="showLoginWin">
           <span>
                欢迎: {{myinfo[usernameColumn]}}
                <a href="#">修改密码</a>
                <a :href="prefix + '/login/logout'">退出</a>
            </span>
        </slot>
        <el-dialog title="登录" :visible.sync="showLoginWin">
          <el-form>
            <el-form-item label="用户名" :label-width="100">
              <el-input v-model="form.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" :label-width="100">
              <el-input v-model="form.password" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="showLoginWin = false">取 消</el-button>
            <el-button type="primary" @click="login">确 定</el-button>
          </div>
        </el-dialog>
    </span>
    `,
    props:["prefix"],
    data(){
        return {
            myinfo: null,
            form:{},
            usernameColumn: null,
            showLoginWin: false
        };
    },
    created(){
        axios.get(this.prefix + "/login/myinfo").then(res=> {
            this.myinfo = res.data.data
            axios.get(this.prefix + "/login/getUsernameColumn").then(res=> {
                this.usernameColumn = res.data;
            })
        });
    },
    methods:{
        login(){
            axios.get(this.prefix + "/login/login",{
                params: this.form
            }).then(res=>{
                if(res.data.code == 1){
                    this.$message(res.data.msg);
                    this.myinfo = res.data.data.user;
                    this.showLoginWin = false;
                    if(res.data.data.goto){
                        location.href = res.data.data.goto;
                    }
                } else {
                    this.$alert(res.data.msg);
                }
            });
        }
    }
})